package com.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.auth.model.wechat.Menu;
import com.auth.vo.wechat.MenuVo;
import com.auth.vo.wechat.TreeObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.mapper.WechatMenuMapper;
import com.wechat.service.WechatMenuService;
import com.wechat.utils.CollectionConvertUtils;
import com.wechat.utils.MenuUtils;
import exception.BusinessException;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author tang
 * @since 2023-07-29
 */
@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, Menu> implements WechatMenuService {
    private final WxMpService wxMpService;
    public WechatMenuServiceImpl(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
    }

    /**
     * 获取所有菜单列表
     *
     * @author Tangjiachang
     * @date 2023/7/29 12:28
     * @param :
     * @return List<MenuVo>
     **/
    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> menuVos = CollectionConvertUtils.menusToMenuVos(baseMapper.selectList(null));
        return MenuUtils.builderTreeMenu(menuVos, 0);
    }

    /**
     * 微信同步菜单
     *
     * @author Tangjiachang
     * @date 2023/7/29 14:54
     * @param :
     * @return void
     **/
    @Override
    public Object syncMenu() {
        JSONArray array = new JSONArray();
        findMenuInfo()
                .stream()
                .forEach(menu -> {
                    final JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name", menu.getName());
                    if (CollectionUtils.isEmpty(menu.getChildren())) {
                        jsonObject.put("type", menu.getType());
                        jsonObject.put("url", "http://oasystem.free.idcfengye.com" + menu.getUrl());
                    } else {
                        JSONArray subButton = new JSONArray();
                        for(TreeObject treeObject : menu.getChildren()) {
                            MenuVo menuVo = (MenuVo) treeObject;
                            JSONObject view = new JSONObject();
                            view.put("type", menuVo.getType());
                            if("view".equals(menuVo.getType())) {
                                view.put("name", menuVo.getName());
                                //H5页面地址
                                view.put("url", "http://oasystem.free.idcfengye.com"+menuVo.getUrl());
                            } else {
                                view.put("name", menuVo.getName());
                                view.put("key", menuVo.getMeunKey());
                            }
                            subButton.add(view);
                        }
                        jsonObject.put("sub_button", subButton);
                    }
                    array.add(jsonObject);
                });
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", array);
        try {
            wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            throw new BusinessException(500,e.getMessage());
        }
        return button;
    }

    /**
     * 移除菜单
     *
     * @author Tangjiachang
     * @date 2023/7/29 15:11
     * @param :
     * @return void
     **/
    @Override
    public void removeMenu() throws WxErrorException {
        wxMpService.getMenuService().menuDelete();
    }

}
